<?php
require_once("../data/db_connection.php");
require_once("../Utils.php");

	$db = new db();
	$user_id = $_GET['user_id'];
	$exist_friend_arr = array();
	$sql1 = "SELECT friend_id FROM contacts WHERE user_id = " . $_GET['user_id'];
	$sql2 = "SELECT _id FROM pTableUserInfo WHERE _id NOT IN ( " . $user_id . ", ";
	if ($result = $db->query($sql1)) {
		while($row = $result->fetch_assoc()) {
			$sql2 .= $row['friend_id'] . ', ';
		}
		$len = strlen($sql2);
		$sql2 = substr($sql2, 0, $len-2);
		$sql2 .= ")";
		echo 'sql2 = ' . $sql2 . '<br />';
	}
?>
<html>
<head>
模拟-Add Contact 
</head>
<body>
<h1>填入id，点击submit，ta就能在你的通讯录里了</h1>
<form id="form1" name="add_contact_form" method="post" action="">
<input type="text" name="user_id" />
选择要添加的朋友：<br />
	<?php 
	if ($result = $db->query($sql2)) {
		while($row = $result->fetch_assoc()) {
			//echo "<option value='" . $row['_id'] . "'>" . $row['_id'] . "</option>";
			echo "<input type='checkbox' name='contacts[]' value='" . $row['_id'] . "'/>" . $row['_id'] . '<br />';
		}
	}
	?>
<input type="submit" name="Submit" value="提交" /> 
</form>
<?php
	$contacts = $_POST['contacts'];
	
	foreach($contacts as $elem) {
		$sql3 = "INSERT INTO contacts VALUES(NULL, " . $user_id . ", ". $elem . ")";
		$return_id = $db->insert_get_id($sql3);
	}
	if ($return_id > 0) {
		echo '添加通讯录朋友成功' . "<br />";
	}
?>
</body>
</html>